package com.alvis.exam.service.impl;

import com.alvis.exam.domain.TStationDict;
import com.alvis.exam.domain.TStationDictExample;
import com.alvis.exam.domain.User;
import com.alvis.exam.repository.TStationDictMapper;
import com.alvis.exam.repository.UserMapper;
import com.alvis.exam.service.StationService;
import com.alvis.exam.viewmodel.admin.station.StationDictPageRequestVM;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StationServiceImpl extends BaseServiceImpl<TStationDict> implements StationService {

    private TStationDictMapper stationMapper;

    @Autowired
    public StationServiceImpl(TStationDictMapper stationMapper){
        super(stationMapper);
        this.stationMapper = stationMapper;
    }

    public List<TStationDict> getAll() {
        TStationDictExample example = new TStationDictExample();
        example.orderBy("province asc");
        return stationMapper.selectByExample(example);
    }

    public List<TStationDict> querySelective(StationDictPageRequestVM model) {
        TStationDictExample example = new TStationDictExample();
        TStationDictExample.Criteria criteria = example.createCriteria();

        if (StringUtil.isNotEmpty(model.getProvince())) {
            criteria.andProvinceLike("%" + model.getProvince() + "%");
        }
        if (StringUtil.isNotEmpty(model.getStationName())) {
            criteria.andStationNameLike("%" + model.getStationName() + "%");
        }

        PageHelper.startPage(model.getPageIndex(), model.getPageSize());
        return stationMapper.selectByExample(example);
    }

    @Transactional
    public void saveOrUpdateBatch(List<TStationDict> list) {
        stationMapper.insertOrUpdateBatch(list);
    }

    @Transactional
    public void deleteAll() {
        TStationDictExample example = new TStationDictExample();
        stationMapper.deleteByExample(example);
    }
}
